import { identity, pickBy, isNumber } from 'lodash';
import request from '@/http/request';
import { IQueryParams } from '@/widgets/table-pagination';

export function getList(queryParams: IQueryParams) {
  let params: any = {
    ...queryParams,
    pageNo: queryParams.pageNum,
    ...pickBy(queryParams.filter, identity),
    ...pickBy(queryParams.filter, isNumber),
  };
  delete params.pageNum;

  return request.get(
    `/admin-api/system/app/page?${new URLSearchParams(params)}`
  );
}

export function getListByOrganize(queryParams: IQueryParams) {
  let params: any = {
    ...queryParams,
    pageNo: queryParams.pageNum,
    ...pickBy(queryParams.filter, identity),
    ...pickBy(queryParams.filter, isNumber),
  };
  delete params.pageNum;

  return request.get(`/admin-api/system/app/get-org-app-page`);
}

export function createItem(entity: any) {
  return request.post(`/admin-api/system/app/create`, entity);
}

export function updateItem(entity: any) {
  return request.put(`/admin-api/system/app/update`, entity);
}

export function uploadFile(file: any) {
  return request.post(`/admin-api/system/app/upload/icon`, file, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  });
}

export function deleteItem(key: any) {
  return request.delete(`/admin-api/system/app/delete?id=${key}`);
}

export function getItemIcon(key: any) {
  return request.post(
    `/admin-api/system/app/get/icon?icon=${key}`,
    {},
    { responseType: 'blob' }
  );
}
